home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / toolalias / source / list.c < prev    next >
C/C++ Source or Header  |  1995-03-27  |  3KB  |  165 lines

  1. /*
  2.  * List operations for ToolAlias.
  3.  * 
  4.  * mws, 4 February 1993
  5.  */
  6. #include <exec/memory.h>
  7. #include <proto/exec.h>
  8. #include <string.h>
  9. #include "list.h"
  10.  
  11. static TOOL *head;    /* empty */
  12.  
  13. void
  14. free_tool(TOOL *tool)
  15. {
  16.     if (tool->oldname) FreeVec(tool->oldname);
  17.     if (tool->newname) FreeVec(tool->newname);
  18.     FreeVec(tool);
  19. }
  20.  
  21. /* get head of list */
  22. TOOL *
  23. get_head()
  24. {
  25.     return head;
  26. }
  27.  
  28. /* get tail of list */
  29. TOOL *
  30. get_tail()
  31. {
  32.     TOOL *tool = head;
  33.  
  34.     while (tool && tool->next)
  35.         tool = tool->next;
  36.     return tool;
  37. }
  38.  
  39. /* add a new entry after given position in list, returning new entry/NULL if failure */
  40. TOOL *
  41. add_tool(TOOL *curtool)
  42. {
  43.     TOOL *newtool;
  44.  
  45.     Forbid();
  46.     if (newtool = AllocVec(sizeof(TOOL), MEMF_CLEAR|MEMF_PUBLIC))
  47.     {
  48.         if (curtool)    /* list non-empty */
  49.         {
  50.             newtool->prev = curtool;
  51.             newtool->next = curtool->next;
  52.             if (curtool->next)
  53.                 curtool->next->prev = newtool;
  54.             curtool->next = newtool;
  55.         }
  56.         else        /* empty list - initialize */
  57.         {
  58.             head = newtool;
  59.         }
  60.     }
  61.     Permit();
  62.     return newtool;
  63. }
  64.  
  65. /* remove current entry, return prev if exists, else next */
  66. TOOL *
  67. rem_tool(TOOL *tool)
  68. {
  69.     TOOL *curtool;
  70.  
  71.     Forbid();
  72.     if (tool->prev)
  73.         tool->prev->next = tool->next;
  74.     else
  75.         /* first tool in list - change head */
  76.         head = tool->next;
  77.  
  78.     if (tool->next)
  79.         tool->next->prev = tool->prev;
  80.  
  81.     /* determine current tool after deletion */
  82.     curtool = tool->prev ? tool->prev : tool->next;
  83.  
  84.     free_tool(tool);
  85.     Permit();
  86.     return curtool;
  87. }
  88.  
  89. /* set/update oldname in tool entry - returns success/failure */
  90. BOOL
  91. set_oldname(TOOL *tool, char *oldname)
  92. {
  93.     char *name;
  94.  
  95.     if (name = AllocVec(strlen(oldname)+1, MEMF_PUBLIC|MEMF_CLEAR))
  96.     {
  97.         Forbid();
  98.         if (tool->oldname)
  99.             FreeVec(tool->oldname);
  100.         strcpy(name, oldname);
  101.         tool->oldname = name;
  102.         Permit();
  103.         return TRUE;
  104.     }
  105.     return FALSE;
  106. }
  107.  
  108. /* set/update newname in tool entry - returns success/failure */
  109. BOOL
  110. set_newname(TOOL *tool, char *newname)
  111. {
  112.     char *name;
  113.  
  114.     if (name = AllocVec(strlen(newname)+1, MEMF_PUBLIC|MEMF_CLEAR))
  115.     {
  116.         Forbid();
  117.         if (tool->newname)
  118.             FreeVec(tool->newname);
  119.         strcpy(name, newname);
  120.         tool->newname = name;
  121.         Permit();
  122.         return TRUE;
  123.     }
  124.     return FALSE;
  125. }
  126.  
  127. /* look-up oldname in list, return newname or NULL if not found */
  128. char *
  129. find_tool(char *name)
  130. {
  131.     TOOL *tool;
  132.  
  133.     Forbid();
  134.     tool = head;
  135.     while (tool)
  136.     {
  137.         if (tool->oldname && *tool->oldname)        /* non-null */
  138.             if (!stricmp(name, tool->oldname))    /* case-insensitive */
  139.             {
  140.                 Permit();
  141.                 return tool->newname;
  142.             }
  143.         tool = tool->next;
  144.     }
  145.     Permit();
  146.     return NULL;
  147. }
  148.  
  149. /* remove whole list from memory */
  150. void
  151. free_list()
  152. {
  153.     TOOL *tool, *next;
  154.  
  155.     Forbid();
  156.     tool = head;
  157.     while (tool)
  158.     {
  159.         next = tool->next;
  160.         free_tool(tool);
  161.         tool = next;
  162.     }
  163.     head = NULL;
  164.     Permit();
  165. }